package com.project.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.project.entity.*;
import com.project.service.AssetAllocateService;
import com.project.service.AssetService;
import com.project.service.MechanismService;
import com.project.service.StaffService;
import com.project.util.PageBean;
import com.project.util.R;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * 资产调拨控制器
 *
 * @author Leo高洋
 * @create 2023-02-24 18:25
 */
@RestController
@RequestMapping("/assetAllocate")
public class AssetAllocateController {
    @Autowired
    private AssetService assetService;

    @Autowired
    private MechanismService mechanismService;

    @Autowired
    private AssetAllocateService assetAllocateService;

    @Autowired
    private StaffService staffService;

    private final static Logger logger = LoggerFactory.getLogger(AssetAllocateController.class);

    /**
     * 获取设备调拨的信息
     *
     * @param pageBean
     * @return
     */
    @PostMapping("/list")
    public R list(@RequestBody PageBean pageBean) {
        logger.info("获取设备调拨的信息");
        Map<String, Object> map = new HashMap<>();
        if (pageBean.getQuery() != null && !pageBean.getQuery().trim().equals("")) {
            map.put("zcmc", pageBean.getQuery().trim());
        }
        map.put("start", pageBean.getStart());
        map.put("pageSize", pageBean.getPageSize());
        List<AssetAllocate> assetAllocateList = assetAllocateService.listAssetAllocate(map);
        for (AssetAllocate assetAllocate : assetAllocateList) {
            // 根据资产调拨对象的资产id对应资产对象的id查询资产信息
            List<Asset> assetList = assetService.list(new QueryWrapper<Asset>().eq("id", assetAllocate.getAssetid())); // mybatis-plus接口
            assetAllocate.setAsset(assetList);
            // 根据资产调拨对象的调拨机构id对应机构对象的id查询机构信息
            Mechanism mechanism = mechanismService.findById(String.valueOf(assetAllocate.getMechanismid()));
            assetAllocate.setMechanism(mechanism);
            // 根据资产调拨对象的使用人id对应员工对象的id查询员工信息
            List<Staff> staffList = staffService.list(new QueryWrapper<Staff>().eq("id", assetAllocate.getStaffid()));
            assetAllocate.setStaff(staffList);
        }
        long total = assetAllocateService.getAssetAllocateTotal(map);
        Map<String, Object> resultMap = new HashMap<>();
        resultMap.put("assetAllocateList", assetAllocateList);
        resultMap.put("total", total);
        return R.ok(resultMap);
    }

    // 新增调拨信息
    @PostMapping("/save")
    public R save(@RequestBody AssetAllocate assetAllocate) {
        logger.info("新增调拨信息");
        if (assetAllocate.getId() == null || assetAllocate.getId() == -1) {
            String lsh = "dblsh" + UUID.randomUUID().toString().substring(0, 5);
            assetAllocate.setLsh(lsh);
            assetAllocateService.save(assetAllocate);
            Asset asset = assetService.getById(assetAllocate.getAssetid());
            asset.setSts(4);
            assetService.saveOrUpdate(asset);
        }
        return R.ok();
    }

    // 调拨入库
    @PostMapping("/putStorage")
    public R putStorage(@RequestBody AssetAllocate assetAllocate) {
        logger.info("调拨入库");
        System.out.println(assetAllocate);
        assetAllocate.setGhrq(new Date());
        Asset asset = assetService.getOne(new QueryWrapper<Asset>().eq("id", assetAllocate.getAssetid()));
        logger.debug(String.valueOf(asset));
        asset.setSts(1);
        assetAllocateService.saveOrUpdate(assetAllocate);
        assetService.saveOrUpdate(asset);
        return R.ok();
    }
}
